package com.gzq.common_core.session;

import com.gzq.common_core.base.App;
import com.gzq.common_core.constant.Constants;
import com.gzq.common_core.utils.KVUtils;

public class MmkvSession implements ISession {
    @Override
    public void setUser(ISessionUser user) {
        KVUtils.put(Constants.KEY_SESSION_USER, App.getGson().toJson(user));
    }

    @Override
    public ISessionUser getUser() {
        return (ISessionUser) KVUtils.get(Constants.KEY_SESSION_USER, ISessionUser.class);
    }

    @Override
    public void setUserToken(ISessionUserToken token) {
        KVUtils.put(Constants.KEY_SESSION_USER, App.getGson().toJson(token));
    }

    @Override
    public ISessionUserToken getUserToken() {
        return (ISessionUserToken) KVUtils.get(Constants.KEY_SESSION_TOKEN, ISessionUserToken.class);
    }

    @Override
    public boolean isLogin() {
        return getUser() != null;
    }

    @Override
    public void clear() {
        KVUtils.remove(Constants.KEY_SESSION_USER);
        KVUtils.remove(Constants.KEY_SESSION_TOKEN);
    }
}
